package com.ruoyi.system.mapper;

import java.util.List;

import com.ruoyi.system.domain.ListDetail;
import com.ruoyi.system.domain.OrderList;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 采购单Mapper接口
 * 
 * @author ruoyi
 * @date 2025-08-15
 */
public interface OrderListMapper 
{

    /**
     * 更新订单总金额=所有详单的总金额
     * @param ids
     */
    @Update("update order_list set price = (select sum(total_price_real) from list_detail where listid = #{id}) where id in ${ids}")
    public void updateOrderPrice(String ids);

    /**
     * 更新订单状态为已支付
     * @param ids
     * @return
     */
    @Update("update order_list set ispay = '1' where id in ${ids}")
    public int checkoutOrder(String ids);

    /**
     * 强制更新订单状态为已收货，给定时器使用
     */
    @Update("update order_list o set o.`status` = '31' ,o.arrive_time = NOW() where o.status = '3' and exists (select id from list_detail d where d.status ='1' and d.listid = o.id)")
    public void  updateOrderListStatusForced31();

    /**
     * 强制更新订单状态为已验收，给定时器使用
     */
    @Update("update order_list o set o.`status` = '4' where o.status = '31' and exists (select id from list_detail d where d.status ='2' and d.listid = o.id)")
    public void  updateOrderListStatusForced4();

    /**
     * 讲全部详单都收货的订单，更新状态为待验收
     */
    @Update("update order_list l set l.`status` = '31',l.arrive_time=now()  where l.`status` = '3' and not exists (select id from list_detail d where d.status = '0' and d.listid = l.id)")
    public void updateOrderListStatus31();

    /**
     * 讲全部详单都验收的订单，更新状态为已验收
     */
    @Update("update order_list l set l.`status` = '4'  where l.`status` = '31' and not exists (select id from list_detail d where d.status <> '2' and d.listid = l.id)")
    public void updateOrderListStatus4();

    /**
     * 批量插入采购单明细
     */
    public int insertListDetailMulity(List<ListDetail> data);

    /**
     * 查询采购单
     * 
     * @param id 采购单主键
     * @return 采购单
     */
    public OrderList selectOrderListById(Long id);

    /**
     * 查询采购单列表
     * 
     * @param orderList 采购单
     * @return 采购单集合
     */
    public List<OrderList> selectOrderListList(OrderList orderList);

    /**
     * 新增采购单,返回采购单id
     * 
     * @param orderList 采购单
     * @return 结果
     */
    public int insertOrderList(OrderList orderList);

    /**
     * 修改采购单
     * 
     * @param orderList 采购单
     * @return 结果
     */
    public int updateOrderList(OrderList orderList);

    /**
     * 删除采购单
     * 
     * @param id 采购单主键
     * @return 结果
     */
    public int deleteOrderListById(Long id);

    /**
     * 批量删除采购单
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteOrderListByIds(Long[] ids);
}
